package com.jf.cloud.seckill.constant;

/**
 * 秒杀信息
 *
 * @author zz
 * @date 2022/11/15
 */
public class RedisLua {

    /**
     * 扣减库存(必须足够扣除的时候才会扣除，如果不够扣除返回负数，如果足够扣除返回剩余库存数量，否则不进行扣减)
     */
    public static final String REDUCE_STOCK_MUST_ENOUGH_SCRIPT  =
            "if (redis.call('exists', KEYS[1]) == 1) then"
                    + "    local stock = tonumber(redis.call('get', KEYS[1]));"
                    + "    local num = tonumber(ARGV[1]);"
                    + "    local results_num = stock - num"
                    + "    if (results_num <= 0) then"
                    + "        return -1;"
                    + "    end;"
                    + "    if (stock >= num) then"
                    + "            return redis.call('incrBy', KEYS[1], 0 - num);"
                    + "        end;"
                    + "    return -2;"
                    + "end;"
                    + "return -3;";
}
